#include "Date.h"

// >运算符重载
bool Date::operator>(const Date& d)
{
    if ((_year > d._year)
        || (_year == d._year && _month > d._month)
        || (_year == d._year && _month == d._month && _day > d._day))      
    {
        return true;
    }
    else
    {
        return false;
    }
}   

// ==运算符重载
bool Date::operator==(const Date& d)  
{
    return _year == d._year
        && _month == d._month
        && _day == d._day;
}

// !=运算符重载
bool Date::operator != (const Date& d)
{
    return !(*this == d);
}

// <运算符重载
bool Date::operator < (const Date& d)
{
    return !(*this >= d);
}

//>=运算符重载
bool Date::operator >= (const Date& d)
{
    return (*this > d) || (*this == d);
}

// <=运算符重载 
bool Date::operator <= (const Date& d)
{
    return !(*this > d);
}

Date Date::operator+(int day) 
{
    Date ret(*this);
    ret += day;
    return ret;
}

Date& Date::operator+=(int day)
{
    _day += day;
    while (_day > GetMonthDay(_year, _month))
    {
        _day -= GetMonthDay(_year, _month);
        _month++;
        if (_month == 13)
        {
            _year++;
            _month = 1;
        }
    }
    return *this;
}                                                                                                                                                                   

void Date::Print()
{
    cout << _year << "-" << _month << "-" << _day << endl;
}

Date& Date::operator++()                                                                                                                                            
{                                                                                                                                                               
    return *this += 1;                                                                                                                                              
}                                                                                                                                                               

Date Date::operator++(int)                                                                                                                                          
{                                                                                                                                                               
    Date tmp(*this);                                                                                                                                                
    *this += 1;                                                                                                                                                 
    return tmp;                                                                                                                                                     
}                                                                                                                                                               

Date& Date::operator-=(int day)                                                                                                                                     
{
    _day -= day;                                                                                                                                                    
    while (_day < 1)                                                                                                                                            
    {                                                                                                                                                           
        _month--;                                                                                                                                               
        if (_month < 1)
        {
            _year--; 
           _month = 12;                                                                                                                                         
        }                                                                                                                                                   
        _day = GetMonthDay(_year, _month) + _day;                                                                                                                   
    }                                                                                                                                                           
    return *this;                                                                                                                                                   
}                                                                                                                                                               

Date Date::operator-(int day) 
{
    Date ret(*this);                                                                                                                                            
    ret -= day;                                                                                                                                                 
    return ret;                                                                                                                                                     
}
                                                                                                                                                                
Date& Date::operator--()                                                                                                                                        
{                                                                                                                                                               
    return *this -= 1;  
}                                                                                                                                                               

Date Date::operator--(int)                                  
{
    Date ret(*this);                            
    *this -= 1;                                                                                                                         
    return ret;                                                                                                                                                 
} 

void TestDate1()
{
    Date d1(2022, 7, 24);
    Date d2(2022, 8, 24);
    
    cout << "(d1 == d2)->" << (d1 == d2) << endl;
    cout << "(d1 > d2) ->" << (d1 > d2) << endl;
    cout << "(d1 < d2) ->" << (d1 < d2) << endl;
    cout << "(d1 >= d2)->" << (d1 >= d2) << endl;
    cout << "(d1 <= d2)->" << (d1 <= d2) << endl;
    cout << "(d1 != d2)->" << (d1 != d2) << endl;
                
    d1 -= 40;
    d1.Print();
    d1 -= 400;
    d1.Print();                                      
    d1 -= 4000; 
    d1.Print();                                              
    (d1 - 40).Print();
    (d1 - 400).Print();
    (d1 - 4000).Print();
    (d1 - 40000).Print();

}

int main()
{
    TestDate1();
    return 0;
}
